//
//  CinemaDataViewController.m
//  代号：47
//
//  Created by lanou3g on 15/7/10.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "CinemaDataViewController.h"
#import "CinemaView.h"
#import "RequestManager.h"
#import "CinemaModel.h"
#import <MAMapKit/MAMapKit.h>
#import <MAMapKit/MAPointAnnotation.h>
#import <AMapSearchKit/AMapSearchAPI.h>
#import "DTViewController.h"


#define APIKey @"c0f24200f0ef6af9359df2356b92c38b"


@interface CinemaDataViewController ()<RequestDelegate,MAMapViewDelegate,AMapSearchDelegate,UIActionSheetDelegate>

@property(nonatomic,retain)CinemaView *cinemaView;
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)MAMapView *mapView;
@property(nonatomic,retain)CinemaModel *model;
@property(nonatomic,retain)AMapSearchAPI *search;




@end

@implementation CinemaDataViewController

-(void)dealloc{

    [_cinemaView release];
    [_scrollView release];
    [_mapView release];
    [_model release];
    [_search release];
    [super dealloc];



}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;//取消自动布局
    self.navigationItem.title = @"影院信息";
    self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height - 64 - 49)] autorelease];
    _scrollView.contentSize = CGSizeMake(kScreen_width, kScreen_height + 1);
    _scrollView.backgroundColor = [UIColor whiteColor];
    self.view = _scrollView;

    [self requestData];
    _cinemaView = [[[CinemaView alloc] initWithFrame:CGRectMake(0, 304, kScreen_width, kScreen_height - 130)] autorelease];
    [self.view addSubview:_cinemaView];
    
}




-(void)initMapView{
    
#pragma ============================================================
    
    UIButton *buttonDT = [UIButton buttonWithType:(UIButtonTypeSystem)];
    UIButton *buttonDH = [UIButton buttonWithType:(UIButtonTypeSystem)];
    buttonDT.backgroundColor = [UIColor blackColor];
    buttonDH.backgroundColor = [UIColor blackColor];
    
    buttonDT.frame = CGRectMake(kScreen_width - 100, 74, 30, 30);
    buttonDH.frame = CGRectMake(kScreen_width - 50, 74, 30, 30);
    
    [buttonDT addTarget:self action:@selector(pushDTViewController) forControlEvents:(UIControlEventTouchUpInside)];
    [buttonDH addTarget:self action:@selector(dadianhua) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:buttonDT];
    [self.view addSubview:buttonDH];
    
#pragma ============================================================
#pragma ============================================================
    
    

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 74, 200, 30)] autorelease];
    label.text = @"位置信息：";
    label.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:label];
    
    
    
    [MAMapServices sharedServices].apiKey = APIKey;
    self.mapView = [[[MAMapView alloc] initWithFrame:CGRectMake(20, 114, kScreen_width - 40, 200)] autorelease];

    _mapView.delegate = self;
    _mapView.layer.borderWidth = 2;
    _mapView.layer.borderColor = [[UIColor blackColor] CGColor];
    _mapView.desiredAccuracy = kCLLocationAccuracyBest;//精度

    [_mapView setZoomLevel:17.1 animated:YES];//地图缩放比例
    
    [self.view addSubview:_mapView];
    
    
    [self dingwei];
    
}
//跳到地图页面
-(void)pushDTViewController{

    DTViewController *DTVC = [[DTViewController alloc] init];
    
    DTVC.str1 = _model.lat;
    DTVC.str2 = _model.lng;
    
    [self presentViewController:DTVC animated:YES completion:^{
        
    }];


}
#pragma =============================打电话========================
//打电话
-(void)dadianhua{  
    NSLog(@"打电话");
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"拨打电话" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:_model.tel otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
//    sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",(long)buttonIndex);
    if (buttonIndex == 0) {
        //拨打电话
        NSString *str = [NSString stringWithFormat:@"tel://%@",_model.tel];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}
#pragma ===========================================================

-(void)dingwei{
    
    //大头针对象
    MAPointAnnotation *pointAnnoyation = [[[MAPointAnnotation alloc] init] autorelease];
    
    //大头针的坐标
    pointAnnoyation.coordinate = CLLocationCoordinate2DMake(_model.lat, _model.lng);
    
    //把大头针添加在地图上
    [_mapView addAnnotation:pointAnnoyation];
    
    //让大头钉居中
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(_model.lat, _model.lng) animated:YES];
    
}
#pragma ==================网络请求========================
-(void)requestData{

    NSString *str = [NSString stringWithFormat:@"http://m.api.tuan800.com/sub/movie20?cmd=GET_ONE_CINEMA&image=iosm2&cinemaId=%@&userId=",_strCZ];
    
    RequestManager *request = [[RequestManager alloc] initWithRequestURL:str requestModel:GET parameter:nil];
    request.delegate = self;
    [request startRequest];

}
-(void)requestDidFinishWithData:(NSData *)data{

    NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
    self.model = [[[CinemaModel alloc] init] autorelease];
    [_model setValuesForKeysWithDictionary:dataDict];
    
    
    
    
    if ([_model.address isEqual:@""] || [_model.guide isEqual:@""]) {
        
        _cinemaView.label1.text = @"暂无信息";
        _cinemaView.label2.text = @"暂无信息";
        
    }else{
        _cinemaView.label1.text = _model.address;
        _cinemaView.label2.text = _model.guide;


    }
    _cinemaView.label1.frame = CGRectMake(20, 40, kScreen_width - 40, [_cinemaView zishiying:_model.address]);
    _cinemaView.labelHD.frame = CGRectMake(20, [_cinemaView zishiying:_model.address] + 30 + 10, kScreen_width - 40, 30);
    _cinemaView.label2.frame = CGRectMake(20, [_cinemaView zishiying:_model.address] + 30 + 20 + 20, kScreen_width - 40, [_cinemaView zishiying:_model.guide]);
    
    [self initMapView];
    
    
}
-(void)requestError:(NSError *)error{


    NSLog(@"%@",error);


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
